<cfcomponent extends="Controller" output="false">
	
	<cffunction name="index">
		<cfset teamPlayers = model("TeamPlayer").findAll()>
	</cffunction>
	
	<cffunction name="show">		
    	<cfset teamPlayers = model("TeamPlayer").findByKey(params.key)>  
	</cffunction>
		
	<cffunction name="new">
		<cfset teamPlayers = model("TeamPlayer").new()>
	</cffunction>
	
	
	<cffunction name="edit">	
		<!--- Find the record --->
    	<cfset teamPlayers = model("TeamPlayer").findByKey(params.key)>
    	
    	<!--- Check if the record exists --->
	    <cfif NOT IsObject(teamPlayers)>
	        <cfset flashInsert(error="TeamPlayer #params.key# was not found")>
			<cfset redirectTo(action="index")>
	    </cfif>
		
	</cffunction>
	
	<cffunction name="create">
		<cfset teamPlayers = model("TeamPlayer").new(params.TeamPlayer)>
		<!--- Verify that the TeamPlayer creates successfully --->
		<cfif teamPlayers.save()>
			<cfset flashInsert(success="The TeamPlayer was created successfully. #TeamPlayer.TeamPlayerid#")>
            <cfset redirectTo(action="index")>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="There was an error creating the TeamPlayer.")>
			<cfset renderPage(action="new")>
		</cfif>
	</cffunction>
	
	<cffunction name="update">
		<cfset teamPlayers = model("TeamPlayer").findByKey(params.key)>
		
		<!--- Verify that the TeamPlayer updates successfully --->
		<cfif teamPlayers.update(params.teamPlayers)>
			<cfset flashInsert(success="The TeamPlayer was updated successfully.")>	
            <cfset redirectTo(action="index")>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="There was an error updating the TeamPlayer.")>
			<cfset renderPage(action="edit")>
		</cfif>
	</cffunction>
	
	<cffunction name="delete">
		<cfset teamPlayers = model("TeamPlayer").findByKey(params.key)>
		
		<!--- Verify that the TeamPlayer deletes successfully --->
		<cfif teamPlayers.delete()>
			<cfset flashInsert(success="The TeamPlayer was deleted successfully.")>	
            <cfset redirectTo(action="index")>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="Error occurred;" & errorMessagesFor("TeamPlayer"))>
			<cfset redirectTo(action="index")>
		</cfif>
	</cffunction>
	
	
	
	
	
	<cffunction name="addTeamPlayers">
		<cfset teamPlayer = model("TeamPlayer").new()>
		<cfset teamPlayer.teamID = params.teamID>
		<cfset chosenPlayers = model("TeamPlayers").findAll(select="PlayerID")>
		<cfset chosenPlayers = ValueList(chosenPlayers.PlayerID)>
		<cfset players = model("Players").findAll(where="PlayerID NOT IN (#chosenPlayers#)")>
	</cffunction>
	
	<!--- CRUD --->
	<cffunction name="registerTeamPlayer">		
		<cfset teamPlayer = model("TeamPlayer").new(params.teamPlayer)>
		
		<cfif teamPlayer.save()>
			<cfset flashInsert(success="The teamPlayer was created successfully.")>
		<cfelse>
			<cfset flashInsert(error="Unable to process you're request " & errorMessagesFor(objectName="teamPlayer",showDuplicates=false) )>			
		</cfif>
		
		<cfset redirectTo(back=true) />		
	</cffunction>
	
	<cffunction name="removeTeamPlayer">
		<cfset teamPlayer = model("TeamPlayer").findByKey(params.key)>
		<cfif isObject(teamPlayer)>
			<cfif teamPlayer.delete()>
				<cfset flashInsert(success="Successfully removed player from team")>
			<cfelse>
				<cfset flashInsert(error="Uh oh, unable to remove player")>
			</cfif>
		<cfelse>
			<cfset flashInsert(info="Oops, cannot find that player")>
		</cfif>
		
		<cfset redirectTo(back=true) />	
	</cffunction>
	
</cfcomponent>